
import UtilsSprite from '../Utils.Sprite/UtilsSprite';

const _defaultBody = Symbol('_defaultBody');
const _sky = Symbol('_sky');
const _add = Symbol('_add');
const _remove = Symbol('_remove');
const _error = Symbol('_error');
export class UtilsLights {
    constructor(_options, _that) {
        if (!_options) return this[_error]('no options?');
        this.options = _options;
        this.options.sky === '0xffffff' || this.options.sky === '0xebc79e' ? this[_defaultBody](_that) : null;
        this[_sky](_that);
    }

    [_sky](_that) {
        _that.lights.enable().setAmbientColor(this.options.sky);
    }

    [_defaultBody](_that) {
        _that.defaultBody1 = _that.lights.addLight(_that.WWC, _that.WHC, 0, 0xffffff)
        .setIntensity(3);
        _that.defaultBody2 = _that.lights.addLight(_that.WFW, _that.WFH, 0, 0xffffff)
        .setIntensity(3);
    }

    [_error](err) {
        throw err;
    }
}

export class LightBody {
    constructor(_data, _that) {
        if (!_data) return this[_error]('no options?');
        this.data = _data;
        this[_add](_that);
    }

    [_add](_that) {
        this.data.forEach(element => {
            _that[element.key] = _that.lights.addLight(element.x, element.y, element.r, element.col)
            .setIntensity(3);
            new UtilsSprite([{
                key: element.sprite.kes,
                spriteKey: element.sprite.spriteKey,
                play: element.sprite.playKey,
                sprXY: {
                    x:  element.x,
                    y:  element.y
                },
                setPipe: true
            }], 'static', _that);
        });
    }

    [_error](err) {
        throw err;
    }
}

export class LightBodyRemove {
    static [_remove](_data, _that) {
        _data.forEach(element => {
            if (!_that[element.key]) return; 
            _that.lights.removeLight(_that[element.key]);
            _that[element.spriteKey].destroy(true, true);
        })
        _that.lights.removeLight(_that.defaultBody1);
        _that.lights.removeLight(_that.defaultBody2);
    }

    static init(_data, _that) {
        LightBodyRemove[_remove](_data, _that);
    }
}